/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.security.controller;

import org.javasimon.aop.Monitored;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.vacoor.nothing.common.util.Threads;
import org.vacoor.nothing.common.validation.Groups;
import org.vacoor.nothing.security.ReloadablePropertySourcesPlaceholderConfigurer;
import org.vacoor.nothing.security.entity.Job;
import org.vacoor.nothing.security.service.IllegalJobException;
import org.vacoor.nothing.security.service.JobManager;
import org.vacoor.nothing.web.controller.ControllerSupport;
import org.vacoor.nothing.web.spring.util.SpringUtils;

import java.io.IOException;
import java.util.Map;

/**
 * 定时作业 Controller
 *
 * @author vacoor
 */
@Controller
@RequestMapping("job")
public class JobController extends ControllerSupport {
    @Autowired
    JobManager jobManager;
    @Value("${dynamic.value}")
    private String ok;
    @Value("${mail.host}")
    private String ok2;

    @Monitored
    @RequestMapping("/")
    public String index(Map<String, Object> model, String h) throws IOException {
        model.put("jobs", jobManager.findAll());
        // SpringUtils.refreshPropertyResourceConfigurer(SpringUtils.getRootApplicationContext());
        // SpringUtils.refresh();
        ApplicationContext ctx = SpringUtils.getRootApplicationContext();
        // FileLocalStorageServiceImpl s = SpringUtils.set(ctx, FileLocalStorageServiceImpl.class, "storageHome", h);
        // System.out.println(s.getStorageHome());
        ReloadablePropertySourcesPlaceholderConfigurer bean = ctx.getBean(ReloadablePropertySourcesPlaceholderConfigurer.class);
        bean.reload();

        return "job/job";
    }

    /**
     * 添加定时作业
     */
    @RequestMapping("add")
    public String add() {
        return "/job/job_add";
    }

    @RequestMapping("create")
    public String create(@Validated(Groups.C.class) Job job, BindingResult err) throws IllegalJobException {
        if (err.hasErrors()) {
            return "/job/job_add";
        }
        jobManager.save(job);
        return redirectViewPath("/");
    }

    @RequestMapping("pause")
    public String pause(Long id) throws IllegalJobException {
        jobManager.pause(id);
        return redirectViewPath("/");
    }

    @RequestMapping("resume")
    public String resume(Long id) throws IllegalJobException {
        jobManager.resume(id);
        return redirectViewPath("/");
    }

    @RequestMapping("trigger")
    public String trigger(Long id) throws IllegalJobException {
        jobManager.trigger(id);
        return redirectViewPath("/");
    }

    public static void test() {
        System.out.println("this is job test: " + Thread.currentThread().getName());
        Threads.sleep(2 * 1000);
        System.out.println("Over: " + Thread.currentThread().getName());
    }

    public void setMailHost(String mailHost) {
        System.out.println(mailHost);
    }
}
